<?php
namespace Admin\Controller\Admin;
//use Admin\Controller\Common\BaseController;
use Think\Controller;
use Think\Model;

class LoginController extends Controller {
    public function _empty(){
        //把所有城市的操作解析到city方法
        echo 404;
    }
//    public function _construct(){
//        \Think\Log::write('测试日志信息，这是警告级别，并且实时写入','WARN');
//        redirect('../../Admin/user/login', 1, '页面跳转中...');
//    }
    public function login(){
        $this->display();

    }

    public function login_out(){
        session_destroy();
    }

    public function loading(){
//        session_start();
        $load_name=I("post.load_name");
        $password=I("post.password");
        $validate_code=md5(strtolower(I("post.validate_code")));

        $verification="";
        if(isset($_SESSION["verification"])){
            $verification=$_SESSION['verification'];
        }
        if($validate_code!=$verification){
            $result['state']=3;
            $result['message']="验证码错误，请重试";
        }else{
            $user=D("Userinfo");
            $user_result=$user->get_login($load_name,md5($password));
            $user_count=count($user_result);
            if($user_count==1){
                $_SESSION['user_id']=$user_result[0]['user_id'];
                $_SESSION['user_name']=$user_result[0]['user_name'];
                $_SESSION['role_id']=$user_result[0]['role_id'];
                $result['state']=1;
                $result['message']="登录成功";
            }else{
                $result['state']=2;
                $result['message']="账号或密码有误";
            }
        }
        $this->ajaxReturn(json_encode($result));
    }
    public function admin(){
//        session_start();
        if(!$_SESSION['user_id']){
            redirect('../login/login', 0, '页面跳转中...');
        }
        $role_id=$_SESSION['role_id'];
        $authority=D('Authority');
        $authority_array_first=$authority->get_authority_first_by_userid($role_id);
        $authority_array_second=$authority->get_authority_second_by_userid($role_id);
        $this->assign('authority_array_first',$authority_array_first);
        $this->assign('authority_array_second',$authority_array_second);
        $this->display();
    }

    public function validate_img(){
//        session_start();
        $str = random(4);

//验证码图片的宽度
        $width  = 100;

//验证码图片的高度
        $height = 35;

//声明需要创建的图层的图片格式
        @ header("Content-Type:image/png");

//创建一个图层
        $im = imagecreate($width, $height);

//背景色
        $back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

//模糊点颜色
        $pix  = imagecolorallocate($im, 187, 230, 247);

//字体色
        $font = imagecolorallocate($im, 41, 163, 238);

//绘模糊作用的点
        mt_srand();
        for ($i = 0; $i < 1000; $i++) {
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
        }
        $font_size = 'Public/fonts/msyh.ttf';
//输出字符
//        imagestring($im, 5, 20, 10, $str, $font);
        imagettftext($im, 20, 0, 20, 27, $font, $font_size, $str);
//输出矩形
        imagerectangle($im, 0, 0, $width -1, $height -1, $font);
//输出图片
        imagepng($im);

        imagedestroy($im);

        $str = md5($str);

//选择 cookie
//SetCookie("verification", $str, time() + 7200, "/");

//选择 Session
        $_SESSION["verification"] = $str;
    }
}